// pages/order/applyrefund/applyrefund.js
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')

Page({
  data: {
    reasonList: ['下错单', '不想要', '商品瑕疵/破损'],
    reasonIndex: -1,
    imgs: [],
    orderId: null
  },
  onLoad: function(options) {
    var that = this
    this.data.orderId = options.orderId;
    http.post({
      url: "Order/getInfo",
      order_id: that.data.orderId,
    }, function(data) {
      console.log(data)
      wx.setNavigationBarTitle({
        title: data.send_time ? "申请售后" : "申请退款"
      })
      that.setData({
        maxMoney: data.send_time ? data.total_fee - data.total_postage : data.total_fee,
        sendTime: data.send_time || '',
        reasonList: data.send_time ? ['不喜欢', '不合适', '不想要了', '有瑕疵', '我家狗狗不喜欢', '其他原因'] : ['多拍', '拍错', '不想要', '缺货']
      })
    })

  },
  bindPickerChange: function(e) {
    this.setData({
      reasonIndex: e.detail.value
    })
  },
  addPic: function(e) {
    var that = this
    if (that.data.imgs.length > 9) {
      util.showModal("最多只能上传10张")
      return false;
    }
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        var imgs = that.data.imgs || []
        imgs.unshift({
          src: tempFilePaths[0],
          cover: true,
          coverHeight: 100
        })
        that.setData({
          imgs: imgs
        })

        var token = wx.getStorageSync('token') || "";
        const uploadTask = wx.uploadFile({
          url: 'https://www.ymqvip.com/api/upload/uploadPic',
          filePath: tempFilePaths[0],
          name: 'imgfile',
          formData: {
            'token': token
          },
          success: function(res) {
            var uploadData = JSON.parse(res.data)
            if (uploadData.code == 0) {
              console.log('上传成功', uploadData.data)
              var imgs = that.data.imgs
              imgs[0].src = uploadData.data
              imgs[0].cover = false
              that.setData({
                imgs: imgs
              })
            } else {
              util.showModal("上传出错，" + res.message)
              return false;
            }
          },
          fail: function(e) {
            console.log('上传出错', e)
            util.showModal("上传出错，请联系管理员。")
            return false;
          }
        })

        uploadTask.onProgressUpdate((res) => {
          var imgs = that.data.imgs
          imgs[0].coverHeight = 100 - res.progress
          that.setData({
            imgs: imgs
          })
        })

      }
    })

  },
  delPic: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var imgs = that.data.imgs
    imgs.splice(index, 1)
    that.setData({
      imgs: imgs
    })
  },
  formSubmit: function(e) {
    var that = this
    var pics = that.data.imgs
    var imgs = []
    for (var i in pics) {
      imgs.push(pics[i].src)
    }
    http.post({
      url: "Order/applyRefund",
      order_id: that.data.orderId,
      reason: that.data.reasonList[e.detail.value.reason] ? that.data.reasonList[e.detail.value.reason] : '',
      info: e.detail.value.info,
      money: e.detail.value.money,
      form_id: e.detail.formId,
      imgs: imgs
    }, function(data) {
      util.showSuccess('申请成功', function() {
        wx.navigateBack({
          delta: 1
        })
      })
    })
  },
})